// pages/login/uinfo.js
const app = getApp();
var utl = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: '0', value: '家政服务' },
      { name: '1', value: '教育培训' },
      { name: '2', value: '咨询服务' },
      { name: '3', value: '技术服务' },
      { name: '4', value: '旅游服务' },
      { name: '5', value: '汽车服务' },
      { name: '6', value: '租赁服务' },
      { name: '7', value: '运动健康' },
      { name: '8', value: '销售渠道' },
      { name: '9', value: '丽人时尚' },
      { name: '10', value: '其他类型' }
    ],
    selectValue:[],
    photos:[],  //图片集合
    uinfo:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uinfo:app.globalData.userInfo
    })
  },
  choosepic:function(e){
    var idex = e.currentTarget.dataset.info;
    var p = this.data.photos;
    var that = this;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        var newArr = p.concat(tempFilePaths);
        that.setData({
          photos: newArr
        })
      }
    })
    
  },
  //删除图片
  deletpic:function(e){
    var index = e.currentTarget.dataset.info;
    var pic = this.data.photos;
    var that = this;
    wx.showActionSheet({
      itemList: ['删除'],
      success(res) {
        pic.splice(index, 1);
        that.setData({
          photos:pic
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    if(e.detail.value.length>3){
      wx.showToast({
        title: '商户类型最多只能选择3种',
        duration:2000,
        icon:'none'
      })
      
      return;
    }
    this.setData({
      selectValue:e.detail.value
    })
  },
  //提交资料
  upspaction:function(){
    var tt = this.data.selectValue;
    var ites = this.data.items;
    if(tt.length == 0){
      wx.showToast({
        title: '请选择商户行业信息',
        duration:2000,
        icon:'none'
      })
      return;
    }
    var descp = this.data.areainput;
    var str = '';
    for (var i in tt){
      var ds = tt[i];
      str = str + ites[ds].value + ';'
    }
    var uinf = this.data.uinfo;
    utl.httServer('USER_MODIFY_INFO', { 'USER_ID': app.globalData.userid, 'SESSION_ID': app.globalData.sessonid, 'NICKNAME': uinf.nickName, 'ICON': uinf.avatarUrl, 'BIZ_TYPE': str, 'BIZ_IMAGE': '','DESCRIPTION':descp }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
      } else {
        wx.navigateTo({
          url: '/pages/regist/userredit',
        })
      }
    })
   
  },
  //业务介绍
  bindTextAreaBlur:function(e){
    this.setData({
      areainput:e.detail.value,
    })
    console.log(e.detail.value)
  },
  //授权获得头像昵称
  onGotUserInfo: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    if (!e.detail.userInfo) return;
    app.globalData.userInfo = e.detail.userInfo;
    console.log(e.detail.rawData)

    this.setData({
      uinfo:e.detail.userInfo
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})